package br.com.dubs.integration;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import br.com.dubs.core.interfaces.ClientHandlerInterface;
import br.com.dubs.persistence.entities.ClientEntity;
import br.com.dubs.persistence.entities.MsgEntity;

@Path("/client")
@Stateless
public class ClientService {
	@EJB
	private ClientHandlerInterface cHandler;

	@Path("/register")
	@POST
	public Boolean register(ClientRest client){
		ClientEntity clientEntity = new ClientEntity();
		clientEntity.setEmail(client.getEmail());
		clientEntity.setLogin(client.getLogin());
		clientEntity.setName(client.getName());
		clientEntity.setPassword(client.getPassword());
		
		return cHandler.addClient(clientEntity);
		
		
		
	}
	@Path("/login")
	@POST
	@Produces("application/json")
	public MsgResponse login(HashMap<String, String> map) {
		List<MsgSimple> responseList = null;
		MsgResponse response = null;
		System.out.println(map.get("login"));
		System.out.println(map.get("passwd"));
		ClientEntity client = cHandler.login(map.get("login"),
				map.get("passwd"));
		if (client == null)
			System.out.println("bla");
		else {
			response = new MsgResponse();
			responseList = new ArrayList<MsgSimple>();
			for (MsgEntity e : client.getReceivedMsg()) {
				responseList.add(new MsgSimple(e.getTitulo(), e.getMsg(), e
						.getSender().getName()));
			}
			response.setMsgList(responseList);

		}

		return response;
	}

	@Path("/teste")
	@GET
	public String teste() {
		ClientEntity client = new ClientEntity();
		client.setLogin("bazinga");
		client.setPassword("bazinga");
		client.setName("Adamastor");
		client.setEmail("jokes@gmail.com");
		
		MsgEntity msg = new MsgEntity();
		msg.setDate(new Date());
		msg.setMsg("Teste 123");
		msg.setTitulo("Teste");
		msg.setReceiver(client);
		msg.setSender(client);
		MsgEntity msg2 = new MsgEntity();
		msg2.setDate(new Date("27/06/1989"));
		msg2.setMsg("Teste 1234");
		msg2.setTitulo("Teste555555555555555555");
		msg2.setReceiver(client);
		msg2.setSender(client);
		MsgEntity msg3 = new MsgEntity();
		msg3.setDate(new Date("25/06/1989"));
		msg3.setMsg("Teste Bazinga");
		msg3.setTitulo("Teste2123131414141413131313121");
		msg3.setReceiver(client);
		msg3.setSender(client);
		client.getReceivedMsg().add(msg);
		client.getReceivedMsg().add(msg2);
		client.getReceivedMsg().add(msg3);
		cHandler.addClient(client);
		return "TESTE POPULADO!!";
	}

	@Path("/msg")
	@GET
	public MsgEntity msg() {
		MsgEntity msg = new MsgEntity();
		// msg.setDate(new Date());
		msg.setMsg("Teste 123");
		msg.setTitulo("Teste");
		return msg;
	}
}
